#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>

#include"cJSON.h"

/*
 * {
 *		"name" : "zhang3",
 *		"age" : 18,
 *		"id" : 200,
 *		"girls" : [
 *			"wangxiaozhang",
 *			"fengjie",
 *			"chunge"
 *			]
 * }
 */

void test_create_json(char *str_out)
{

	cJSON* root;//{}
	cJSON* girls_array;//[]

	root = cJSON_CreateObject();//{}

	//name:zhang3 ---> {}
	cJSON_AddStringToObject(root, "name", "zhang3");
	//cJSON_AddItemToObject(root, "name", "zhang3");
	//age:18 ---> {}
	cJSON_AddNumberToObject(root, "age", 18);
	//id:200 ---> {}
	cJSON_AddNumberToObject(root, "id", 200);
	
	// girls:[] 
	girls_array = cJSON_CreateArray();
	// wangxiaozhang ---> []
	// ...
	cJSON_AddItemToArray(girls_array, cJSON_CreateString("wangxiaozhang"));
	cJSON_AddItemToArray(girls_array, cJSON_CreateString("fengjie"));
	cJSON_AddItemToArray(girls_array, cJSON_CreateString("chunge"));
	// girls:[] ---> {}
	cJSON_AddItemToObject(root, "girls", girls_array);

	//root
	char *out = NULL;

	out = cJSON_Print(root);
	cJSON_Delete(root);

	strcpy(str_out, out);
	free(out);

}

void test_parse_json(char *str)
{
	cJSON* root = NULL;

	//将json字符串 转换成 内存 cjson格式中
	root = cJSON_Parse(str);

	cJSON* name;
	name = cJSON_GetObjectItem(root, "name");

	printf("%s = %s \n", name->string, name->valuestring);

	cJSON* age;
	age = cJSON_GetObjectItem(root, "age");
	printf("%s = %d \n", age->string, age->valueint);

	cJSON* id;
	id = cJSON_GetObjectItem(root, "id");
	printf("%s = %d \n", id->string, id->valueint);

	cJSON* girls_array;
	girls_array = cJSON_GetObjectItem(root, "girls");
	int num = cJSON_GetArraySize(girls_array);
	int i = 0;
	for (i = 0; i < num; i++) {
		cJSON *temp = cJSON_GetArrayItem(girls_array, i);
		printf("array[%d]:%s\n", i, temp->valuestring);
	}

	cJSON_Delete(root);

}

int main()
{
	char str[4096] = {0};

	//创建一个json格式数据
	test_create_json(str);

	printf("%s\n", str);

	printf("+++++++++++++++++++++\n");
	//解析一个json格式数据
	test_parse_json(str);

	return 0;
}
